<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
// 
//  ajax_model.php
//  www.6block.com
//  
//  Created by Will_Lee on 2011-05-18.
//  Copyright 2011 lishuzu. All rights reserved.
// 

class Ajax_model extends CI_Model
{
		function __construct(){
			parent::__construct();
			$this->userinfo = $this->session->userdata('user');
			// 评论模块
			$this->load->model('Comment_model');
			//关系模块
			$this->load->model('ship_model');
			// 喜欢模块
			$this->load->model('Collection_model');
			// 安全模块
	 		$this->load->model('common/security_model');
			// 动态模块
			$this->load->model('feed_model');
			// 我说模块
			$this->load->model('talk_model');
			// 作品模块
			$this->load->model('work_model');
			//提醒模块
			$this->load->model('notification_model');
		}
		
		function get_comments($id, $idtype){
			$data = $comments = array();
			$comments = $this->Comment_model->get_comments($id, $idtype);
			foreach ($comments['list'] as $comment) {
				$comment['avatar_small'] = avatar($comment['authorid'], 'small');
				$data[] = $comment;
			}
			return $data;
		}
		
		//添加评论
		function add_comment($id, $idtype, $message){
			if($id && $idtype && $message){
				$res = $this->Comment_model->add_comment($id, $idtype, $message);
				return $res;
			}
			return false;
		}
		
		//删除评论
		function del_comment($cid){
			if ($cid) {
				$comment = $this->Comment_model->get_comment_by_cid($cid);
				if ($comment['uid'] == $this->userinfo['uid'] || $comment['authorid'] == $this->userinfo['uid']) {
					$result = $this->Comment_model->delete_comment($cid);
					//减少feed评论数
					$this->db->query("UPDATE ".$this->db->dbprefix('feed')." SET replynum = replynum - 1 WHERE id='".$comment['id']."' AND idtype='".$comment['idtype']."'");
					//减少我说评论数
					$this->db->query("UPDATE ".$this->db->dbprefix('talk')." SET replynum = replynum - 1 WHERE talkid='".$comment['id']."'");
					return '1';
				}else{
					return '2';
				}
			}
			return '3';
		}
		// 喜欢
		function add_collection($collection){
			if ($collection) {
				$mycollection = $this->Collection_model->get_Collection_by_wid($collection['wid']);
				if ($mycollection) {
					$data = array(
							'code' => '2',
							'collection' => $mycollection
						);
					return $data;
				}
				$insert_id = $this->Collection_model->insert_collection($collection);
				// 增加喜欢数
				if ($collection['wid']) {
					$wid = intval($collection['wid']);
					$this->db->query("UPDATE ".$this->db->dbprefix('works')." SET likenum = likenum + 1 WHERE wid='".$wid."'");
					$this->db->query("UPDATE ".$this->db->dbprefix('user')." SET likenum = likenum + 1 WHERE uid='".$this->userinfo['uid']."'");
				}

				// 更新session
				$this->security_model->update_user_session();

				$data = array(
						'code' => '1',
						'insert_id' => $insert_id
					);
				return $data;
			}
			return false;
		}
		
		// 取消喜欢
		function del_collection($wid){
			$wid = intval($wid);
			
			$uid = $this->userinfo['uid'] ? $this->userinfo['uid'] : '';
			if (!$uid) {
				$data = array(
						'code' => '2',
					);
				return $data;
			}
			if ($wid) {
				$result = $this->Collection_model->delete_collection($wid);
				if ($result) {
					// 减少喜欢数
					if ($wid) {
						$this->db->query("UPDATE ".$this->db->dbprefix('works')." SET likenum = likenum - 1 WHERE wid='".$wid."'");
						$this->db->query("UPDATE ".$this->db->dbprefix('user')." SET likenum = likenum - 1 WHERE uid='".$this->userinfo['uid']."'");
					}

					// 更新session
					$this->security_model->update_user_session();

					$data = array(
							'code' => '1',
						);
					return $data;
				}else{
					$data = array(
							'code' => '4',
						);
					return $data;					
				}
			}else{
				$data = array(
						'code' => '3',
					);
				return $data;				
			}

		}
		// 加关注
		function follow($ship){
			if ($ship) {
				$myship = $this->ship_model->get_follow($ship['followid']);
				if ($myship) {
					$data = array(
							'code' => '2',
							'collection' => $myship
						);
					return $data;
				}
				$insert_id = $this->ship_model->add($ship);
				// 增加关注数量
				if ($ship['uid']) {
					$this->db->query("UPDATE ".$this->db->dbprefix('user')." SET follow_count = follow_count + 1 WHERE uid='".$this->userinfo['uid']."'");
					//加粉丝
					$this->db->query("UPDATE ".$this->db->dbprefix('user')." SET fans_count = fans_count + 1 WHERE uid='".$ship['followid']."'");
				}

				// 更新session
				$this->security_model->update_user_session();
				
				$data = array(
						'code' => '1',
						'insert_id' => $insert_id
					);
				return $data;
			}
			return false;
		}
		
		// 取消关注
		function unfollow($ship){
			$uid = $this->userinfo['uid'] ? $this->userinfo['uid'] : '';
			if (!$uid) {
				// 未登陆
				$data = array(
						'code' => '2',
					);
				return $data;
			}
			
			if ($ship) {
				$myship = $this->ship_model->get_follow($ship['followid']);
				if ($myship) {
					$this->ship_model->delete($myship['id']);
					// 增加关注数量 给别人增加粉丝数
					if ($ship['uid']) {
						$this->db->query("UPDATE ".$this->db->dbprefix('user')." SET follow_count = follow_count - 1 WHERE uid='".$this->userinfo['uid']."'");
						//减粉丝
						$this->db->query("UPDATE ".$this->db->dbprefix('user')." SET fans_count = fans_count - 1 WHERE uid='".$ship['followid']."'");
					}

					// 更新session
					$this->security_model->update_user_session();
					
					// 取消关注成功
					$data = array(
							'code' => '1',
						);
					return $data;
				}else {
					// 未找到关系
					$data = array(
							'code' => '3',
						);
					return $data;
				}
			}
			// 缺少参数
			$data = array(
					'code' => '4',
				);
			return $data;
		}
}
?>